using GLib;
using Gtk;

namespace Zoo
{

public class XWidget : DrawingArea
{
    public X.Display dpy { get; set; }
    public X.Window x_window { get; set; }
    public string dpy_name { get; set; }
    public string bin_path { get; set; }
    public string bin_options { get; set; }

    private Pid xserver_pid;
    private int x_display;

    public XWidget()
    {
        this.width_request = 800;
        this.height_request = 480;
        this.xserver_pid = (Pid)0;
        this.x_display = 4;

        this.button_press_event += this.on_button_event;
        this.button_release_event += this.on_button_event;
        this.key_press_event += this.on_key_event;
        this.key_release_event += this.on_key_event;
    }

    public bool start_xserver()
    {
        string[] args = {"Xephyr", ":%d".printf(this.x_display),
                         "-host-cursor", "-dpi", "96", "-ac", "-kb", "-parent",
                         "%li".printf(Gdk.x11_drawable_get_xid(this.window))
                        };

        try {
            Process.spawn_async("/usr/bin", args, null,
                                SpawnFlags.SEARCH_PATH,
                                null, out this.xserver_pid);
        } catch (Error e) {
            warning("Failed to Launch %s: %s\n", args[0], e.message);
        }

        if (((int) this.xserver_pid) == -1) {
            error("'%s' Did not start correctly.", string.joinv(" ", args));
        }

        return false;
    }

    public void stop_xserver()
    {
        if (((int) this.xserver_pid) > 0)
            Posix.kill(((int) this.xserver_pid), Posix.SIGTERM);
    }

    private bool on_button_event(Gdk.EventButton button)
    {
        stderr.printf("on_button_event\n");
        grab_focus();
        return false;
    }

    private bool on_key_event(Gdk.EventKey event)
    {
        if (this.has_focus) {
            /*
            XEvent xevent;
            xevent.xkey.type = (event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
            xevent.xkey.window = GDK_WINDOW_XWINDOW(GTK_WIDGET(xwidget->darea)->window);
            xevent.xkey.root = GDK_WINDOW_XWINDOW(gdk_screen_get_root_window
                  (gdk_drawable_get_screen(GTK_WIDGET(xwidget->darea)->window)));
            xevent.xkey.time = event->time;

            xevent.xkey.x = 0;
            xevent.xkey.y = 0;
            xevent.xkey.x_root = 0;
            xevent.xkey.y_root = 0;
            xevent.xkey.state = event->state;
            xevent.xkey.keycode = event->hardware_keycode;
            xevent.xkey.same_screen = TRUE;

            gdk_error_trap_push();
            XSendEvent (GDK_WINDOW_XDISPLAY(GTK_WIDGET(xwidget->darea)->window),
                        xwidget->window, False, NoEventMask, &xevent);

            gdk_display_sync(gtk_widget_get_display(widget));

            if (gdk_error_trap_pop())
                g_warning("X error on XSendEvent");
            */
            return true;
        }

        return false;
    }

}

} // namespace Zoo

